package com.hibernate.hrm.controller.contract;

import com.hibernate.hrm.dao.ContractRepository;
import com.hibernate.hrm.entity.Contract;
import com.hibernate.hrm.entity.User;
import com.hibernate.hrm.service.login.OperateLogService;
import com.hibernate.hrm.util.ExcelExportUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/contract")
public class ContractExportController {

    private final ContractRepository contractRepository;
    private final OperateLogService operateLogService;

    public ContractExportController(ContractRepository contractRepository,
                                    OperateLogService operateLogService) {
        this.contractRepository = contractRepository;
        this.operateLogService = operateLogService;
    }

    @GetMapping("/export")
    public void exportContractsToExcel(HttpServletResponse response,
                                       @AuthenticationPrincipal User currentUser,
                                       HttpServletRequest request) throws IOException {
        List<Contract> contracts = contractRepository.findAll();
        ExcelExportUtil.exportContractsToExcel(contracts, response);
        // 记录导出日志
        operateLogService.recordExportLog(currentUser, contracts.size(), request);
    }
}
